package com.zgroup.controllers.admin

import com.zgroup.domain.Estate

class EstateController extends AdminDispatchController {
  def estateService
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  def beforeInterceptor = [
          action: this.&auth
  ]

  def auth() {
    return super.auth()
  }

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [estateInstanceList: Estate.list(params), estateInstanceTotal: Estate.count()]
  }

  def estatePic = {
    def forward = params.forward
    def view = "/estate/estatePic"
    def list = estateService.estatePic(Estate.get(params.id))
    flash.okFlag = params.okFlag
    flash.estateId = params.id
    flash.type = params.type
    render(view: view, model: [list: list, forward: forward])
  }

  def storage = {
    if (estateService.storage(params.id, params.estateId, params.infoId)) {
      render true
      return
    }
    render false
  }

  def create = {
    def estateInstance = new Estate(params)
    return [estateInstance: estateInstance]
  }

  def save = {
    def estateInstance = new Estate(params)
    if (estateInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'estate.label', default: 'Estate'), estateInstance.id])}"
      redirect(action: "show", id: estateInstance.id)
    }
    else {
      render(view: "create", model: [estateInstance: estateInstance])
    }
  }

  def show = {
    def estateInstance = Estate.get(params.id)
    if (!estateInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estate.label', default: 'Estate'), params.id])}"
      redirect(action: "list")
    }
    else {
      [estateInstance: estateInstance]
    }
  }

  def edit = {
    def estateInstance = Estate.get(params.id)
    if (!estateInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estate.label', default: 'Estate'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [estateInstance: estateInstance]
    }
  }

  def update = {
    def estateInstance = Estate.get(params.id)
    if (estateInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (estateInstance.version > version) {

          estateInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'estate.label', default: 'Estate')] as Object[], "Another user has updated this Estate while you were editing")
          render(view: "edit", model: [estateInstance: estateInstance])
          return
        }
      }
      estateInstance.properties = params
      if (!estateInstance.hasErrors() && estateInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'estate.label', default: 'Estate'), estateInstance.id])}"
        redirect(action: "show", id: estateInstance.id)
      }
      else {
        render(view: "edit", model: [estateInstance: estateInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estate.label', default: 'Estate'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def estateInstance = Estate.get(params.id)
    if (estateInstance) {
      try {
        estateInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'estate.label', default: 'Estate'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'estate.label', default: 'Estate'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estate.label', default: 'Estate'), params.id])}"
      redirect(action: "list")
    }
  }
}
